<template>
  <div class="body">
    <el-button @click="add_warehouse" style="margin-left: 15px"
      >新增仓库</el-button
    >
    <el-table :data="productList"  v-loading="loading" stripe :row-key="funid" :default-expand-all="isExpandAll"
      :tree-props="{children: 'children'}"  border>
      <el-table-column prop="name" label="仓库名称">
      </el-table-column>
      <!-- <el-table-column label="仓库分类">
        <template slot-scope="scope">
          {{ scope.row.warehouse_type?scope.row.warehouse_type.warehouse_type:''}}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="260" fixed="right">
        <template slot-scope="scope">
          <el-button  v-if="!scope.row.warehouse_id"
            @click="set_product(scope.row)"
            type="text"
            icon="el-icon-edit"
            >编辑仓库</el-button>
            <el-button  v-if="scope.row.warehouse_id"
            @click="set_huojia(scope.row,2)"
            type="text"
            icon="el-icon-edit"
            >编辑货架</el-button>
          <el-button
            @click="set_huojia(scope.row,1)" v-if="!scope.row.warehouse_id"
            type="text"
            icon="el-icon-edit"
            >添加子货架</el-button>
            <!-- <el-button
            @click="set_cangwei(scope.row, 1)"
            v-if="scope.row.warehouse_id && !scope.row.goods_shelf_id"
            type="text"
            icon="el-icon-edit"
            >添加仓位</el-button
          > -->
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="是否删除这条数据？"
            @confirm="del_product(scope.row)"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="30%"
      :close-on-click-modal="false"
      @close="close_dialog"
    >
      <el-row style="display: flex; align-items: center">
        <el-col :span="3"> 名称 </el-col>
        <el-col :span="19">
          <el-input v-model="warehouse_name" placeholder="请输入名称"></el-input>
        </el-col>
      </el-row>
      <!-- <el-row style="display: flex; align-items: center;margin-top:15px">
        <el-col :span="3"> 分类 </el-col>
        <el-col :span="19">
          <el-select
                v-model="warehouse_type"
                placeholder="请选择分类"
                style="width: 100%"
              >
                <el-option
                  v-for="item in WarehouseTypeList"
                  :key="item.id"
                  :label="item.warehouse_type"
                  :value="item.id"
                >
                </el-option>
              </el-select>
        </el-col>
      </el-row> -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd()">确定</el-button>
      </span>
    </el-dialog>

    

    <el-dialog
      :title="huojiatitle"
      :visible.sync="ishuojia"
      width="40%"
      :close-on-click-modal="false"
      @close="close_dialog"
    >
      <el-row style="display: flex; align-items: center">
        <el-col :span="3"> 货架名称 </el-col>
        <el-col :span="19">
          <el-input v-model="addhuojia.goods_shelf_name"></el-input>
        </el-col>
      </el-row>

      <span slot="footer" class="dialog-footer">
        <el-button @click="ishuojia = false">取 消</el-button>
        <el-button type="primary" @click="submitaddhuojia()">确定</el-button>
      </span>
    </el-dialog>


    <el-dialog
      :title="cangwei_title"
      :visible.sync="iscangwei"
      width="40%"
      :close-on-click-modal="false"
      @close="close_dialog"
    >
      <el-row style="display: flex; align-items: center">
        <el-col :span="3"> 仓位名称 </el-col>
        <el-col :span="19">
           <el-input v-model="addcangwei.warehouse_location"></el-input>
        </el-col>
      </el-row>

      <span slot="footer" class="dialog-footer">
        <el-button @click="iscangwei = false">取 消</el-button>
        <el-button type="primary" @click="submitaddcangwei()">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { getWarehouseList,createWarehouseLocation, createWarehouse ,createGoodsShelf , delWarehouse , delGoodsShelf,getWarehouseTypeList} from "@/api/product/index";
export default {
  data() {
    return {
      cangwei_title:'',
      iscangwei:false,
      loading:true,
      huojiatitle:'',
      title: "",
      isProduct: false,
      ishuojia:false,
      productList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      warehouse_name: "",
      id: "",
      addhuojia:{
        goods_shelf_name:'',
        goods_shelf_id:'',
        warehouse_id:'',
      },
      addcangwei:{
        goods_shelf_id:'',
        warehouse_id:'',
        warehouse_location:''
      },
      isExpandAll: false,
      WarehouseTypeList:[]
    };
  },
  mounted() {
    this.getlist();
    this.get_WarehouseTypeList()
  },
  methods: {
    funid(row){
      if(row.id){
        return row.id+'a'
      }else{
        return row.goods_shelf_id+'b'
      }
    },
    set_cangwei(row){
      this.addcangwei.goods_shelf_id=row.id
      this.addcangwei.warehouse_id=row.warehouse_id
      console.log(row)
      this.cangwei_title='添加仓位',
      this.iscangwei=true
    },
    submitaddcangwei(){
      if(this.addcangwei.warehouse_location==''){
          this.$message({
            type: "error",
            message: "请输入仓位名称",
          });
          return;
      }
      createWarehouseLocation(this.addcangwei).then(res=>{
        if(res.code==200){
            this.$message({
              type: "success",
              message: "添加成功",
            });
            this.iscangwei=false 
            this.getlist();
        }
      })
    },
    get_WarehouseTypeList(){
      getWarehouseTypeList().then(res=>{
        if(res.code==200){
             this.WarehouseTypeList=res.data.data
        }
      })
    },
    set_huojia(row,type){
      console.log(row)
      this.ishuojia=true
      if(type==1){
        this.addhuojia.warehouse_id=row.id 
        this.addhuojia.goods_shelf_name=row.name
        this.huojiatitle='新增子货架'
      }else{ 
        this.addhuojia.warehouse_id=row.warehouse_id
        this.addhuojia.goods_shelf_id=row.goods_shelf_id  
        this.addhuojia.goods_shelf_name=row.name
        this.huojiatitle='编辑货架'
      }
      
    },
    submitaddhuojia() {
      if (this.addhuojia.goods_shelf_name == "") {
        this.$message({
          type: "error",
          message: "请输入货架名称",
        });
        return;
      }
      createGoodsShelf(this.addhuojia).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "创建成功",
          });
          this.ishuojia=false
          this.getlist();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    add_warehouse() {
      this.title = "新增仓库";
      this.isProduct = true;
    },
    set_product(row){
        // if(row.warehouse_id){

        // }
      if(row.warehouse_id){
        this.title = "编辑货架";
        this.addhuojia.goods_shelf_name=row.name
      }else{
        this.title = "编辑仓库";
      }   
      this.isProduct = true;
      this.id = row.id;
      this.warehouse_name=row.name;
      // this.warehouse_type=row.warehouse_type.warehouse_type;
    },
    del_product(row){
      console.log(row)
      if(row.goods_shelf_id){
        delGoodsShelf({id:row.goods_shelf_id,is_del:1}).then(res=>{
          if(res.code==200){
            this.$message({
              type: "success",
              message: "删除成功",
            });
           this.addhuojia={
              goods_shelf_name:'',
              goods_shelf_id:'',
              warehouse_id:''
            },
            this.getlist();
          }else{
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
      }else{
        delWarehouse({id:row.id,is_del:1}).then(res=>{
          if(res.code==200){
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getlist();
          }else{
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
      }   

    },
    close_dialog(){

    },
    submitadd() {
      if (this.warehouse_name == "") {
        this.$message({
          type: "error",
          message: "请输入仓库名称",
        });
        return;
      }
      createWarehouse({
        id: this.id,
        warehouse_name: this.warehouse_name,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "创建成功",
          });
          this.isProduct = false;
          this.getlist();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    getlist() {
      getWarehouseList({...this.pagination,...this.ser}).then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;     
          this.total = res.data.total;
          this.loading = false;  
        }
      });
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
  },
};
</script>

<style scoped></style>